﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Windows.Documents;
using System.IO;

namespace 流量控制试验台
{
    public partial class 变频泵 : Form
    {
        public 变频泵()
        {
            InitializeComponent();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            历史数据 form = new 历史数据(); form.Show();   
        }

        private void button7_Click(object sender, EventArgs e)
        {
            过程曲线 form = new 过程曲线(); form.Show();  
        }

        private void button8_Click(object sender, EventArgs e)
        {
            this.Hide(); 控制选择 form = new 控制选择(); form.Show();
        }

        private void rectangleShape1_Click(object sender, EventArgs e)
        {

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {

        }
        SerialPort com;
        private void button5_Click(object sender, EventArgs e)
        {
            /************************************************************************/
            /* wf                                                                   */
            /************************************************************************/
            //string s = (-5.32).ToString("00.000");
            com = new SerialPort(comboBox1.Text);
            try
            {
                com.Open();
            }
            catch
            {
                MessageBox.Show("串口无法打开");
                return;
            }
            try
            {
                pidProc = new PID();
                pidProc.pidParam.pgain = float.Parse(textBox3.Text);
                pidProc.pidParam.sp = float.Parse(textBox1.Text);
                if (radioButton1.Checked)//p
                {
                    //pidProc.pidParam.pgain = float.Parse(textBox3.Text);
                }
                else if (radioButton2.Checked)//pi
                {
                    pidProc.pidParam.igain = float.Parse(textBox5.Text);
                }
                else if (radioButton3.Checked)//pd
                {
                    pidProc.pidParam.dgain = float.Parse(textBox4.Text);
                }
                else//pid
                {
                    pidProc.pidParam.igain = float.Parse(textBox5.Text);
                    pidProc.pidParam.dgain = float.Parse(textBox4.Text);
                }
            }
            catch
            {
                MessageBox.Show("输入的参数不正确");
                return;
            }
            timer1.Interval = int.Parse(textBox7.Text);
            //timer1.Enabled = true;           
            dam = new DAM(com);
            flowList = new List<float>();
            /************************************************************************/
            /*                                                                      */
            /************************************************************************/
            dam.ProcDA1(0);
            oldDA = 0;
            timer1_Tick(null,null);
            button3.BackColor = Color.Blue;
            button2.BackColor = Color.Blue;
            button1.Enabled = true;
            textBox1.Enabled = false;
            radioButton1.Enabled = false;
            radioButton2.Enabled = false;
            radioButton3.Enabled = false;
            radioButton4.Enabled = false;
            button5.Enabled = false;
            button8.Enabled = false;
            textBox3.Enabled = false;
            textBox4.Enabled = false;
            textBox5.Enabled = false;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            button3.BackColor = Color.Red ;
            button2.BackColor = Color.Red ;
            button6.Enabled = true;
            button7.Enabled = true;
            textBox1.Enabled = true;
            radioButton1.Enabled = true;
            radioButton2.Enabled = true;
            radioButton3.Enabled = true;
            radioButton4.Enabled = true;
            button5.Enabled = true;
            button8.Enabled = true;
            textBox3.Enabled = true;
            textBox4.Enabled = true;
            textBox5.Enabled = true;
            /************************************************************************/
            /*                                                                      */
            /************************************************************************/
            timer1.Enabled = false;

            com.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }
        /************************************************************************/
        /* wf                                                                   */
        /************************************************************************/
        PID pidProc;
        List<float> flowList;
        DAM dam;
        float oldDA;
        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            byte[] rx;
            //Modbus.TargetAddr = 1;
            //Modbus.ProcCommand03(3, 1, out rx);
            //float flow=rx[3];
            float flow = dam.Proc7401();
            if (float.IsNaN(flow))//读取错误
            {
                MessageBox.Show("无法读取流量数据");
                timer1.Enabled = true;
                return;
            }
            flow = flow - 4;
            flow = flow / 16;
            flow = flow * 27.5f;
            flow = flow + 2.5f;
            chart1.Series[0].Points.AddY(flow);
            flowList.Add(flow);
            textBox2.Text = flow.ToString("f3");
            float pid=pidProc.PIDCalc(flow);
            pid = oldDA + pid;
            oldDA = pid;
            textBox6.Text = pid.ToString("00.000");
            /************************************************************************/
            /* 20为最大，4为最小，你看着改                                          */
            /************************************************************************/
            if (pid > 20)
                pid = 20;
            else if (pid < 4)
                pid = 4;
            if (!dam.ProcDA1(pid))
            {
                MessageBox.Show("无法设定泵的工作");
                timer1.Enabled = true;
                return;
            }
            timer1.Enabled = true;
        }

        private void 变频泵_Load(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            Array.Sort(ports);
            comboBox1.Items.Clear();
            comboBox1.Items.AddRange(ports);
            comboBox1.SelectedIndex = 0;
            txt.AddTextLog("要写入的文本的东西");
        }
        public void AddTextLog(string strLog)
        {
            DateTime dt = DateTime.Now;
            string file = System.Windows.Forms.Application.StartupPath + "\\TextLog\\" + String.Format("{0:D4}", dt.Year) + "-" + String.Format("{0:D2}", dt.Month) + "-" + String.Format("{0:D2}", dt.Day) + "运行信息.txt";
            FileInfo f = new FileInfo(file);
            // 如果文件所在的文件夹不存在则创建文件夹
            if (!Directory.Exists(f.DirectoryName))
                Directory.CreateDirectory(f.DirectoryName);
            FileStream fs;
            try
            {
                fs = new FileStream(file, FileMode.Open);
            }
            catch (FileNotFoundException)
            {
                fs = new FileStream(file, FileMode.Create);
            }
            catch
            {
                return;
            }
            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            fs.Seek(0, SeekOrigin.End);
            sw.WriteLine(strLog);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();/**/
        }
    }
}
